using System;
using System.Collections.Generic;
using System.Text;
using PortalCurriculo.Util.Encryption;

namespace PortalCurriculo.Util.Encryption
{
    public class Encryption
    {
        public string IV { get; set; }
        public string Key { get; set; }

        /// <summary>
        /// Cryptographs any characters or numbers
        /// </summary>
        /// <param name="text">String to be cryptographed</param>
        /// <returns>Cryptographed text</returns>
        public string Cryptograph(string text)
        {
            try
            {
                var enc = new Encryptor(EncryptTransformer.EncryptionAlgorithm.Des);
                byte[] plainText = Encoding.ASCII.GetBytes(text);

                byte[] key = Encoding.ASCII.GetBytes(Key);
                byte[] iv = Encoding.ASCII.GetBytes(IV);

                enc.IV = iv;

                byte[] cipherText = enc.Encrypt(plainText, key);

                iv = enc.IV;
                key = enc.Key;

                return Convert.ToBase64String(cipherText);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cipherText">Text cryptographed is decryptographed. Param must be <c>Convert.FromBase64String</c>.</param>
        /// <returns>Decryptographed Text</returns>
        public string Decryptograph(string texto)
        {
            // Set the required algorithm 
            EncryptTransformer.EncryptionAlgorithm algorithm = EncryptTransformer.EncryptionAlgorithm.Des;

            byte[] iv = null;
            byte[] key = null;

            key = Encoding.ASCII.GetBytes(Key);
            iv = Encoding.ASCII.GetBytes(IV);

            try
            {
                byte[] cipherText = Convert.FromBase64String(texto);
                Decryptor dec = new Decryptor(algorithm);
                dec.IV = iv;

                byte[] plainText = dec.Decrypt(cipherText, key);

                return Encoding.ASCII.GetString(plainText);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public void teste()
        {
            // Set the required algorithm 
            EncryptTransformer.EncryptionAlgorithm algorithm = EncryptTransformer.EncryptionAlgorithm.Des;
            // Init variables. 
            byte[] iv = null;
            byte[] cipherText = null;
            byte[] key = null;

            key = Encoding.ASCII.GetBytes("password");
            iv = Encoding.ASCII.GetBytes("init vec");
            cipherText = Encoding.ASCII.GetBytes("fsd");

            try
            {
                Decryptor dec = new Decryptor(algorithm);
                dec.IV = iv;

                byte[] plainText = dec.Decrypt(cipherText, key);

                string a = "Plain text: " + Encoding.ASCII.GetString(plainText);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception decrypting. " + ex.Message);
                return;
            }
        }

        public void teste2()
        {
            // Set the required algorithm 
            EncryptTransformer.EncryptionAlgorithm algorithm = EncryptTransformer.EncryptionAlgorithm.Des;
            // Init variables. 
            byte[] iv = null;
            byte[] cipherText = null;
            byte[] key = null;
            try
            { 
                Encryptor enc = new Encryptor(EncryptTransformer.EncryptionAlgorithm.Des);
                byte[] plainText = Encoding.ASCII.GetBytes("Test String");
                
                key = Encoding.ASCII.GetBytes("$pc#kc8@");
                iv = Encoding.ASCII.GetBytes("$pc#ivc@");
                
                enc.IV = iv;
                // Perform the encryption.
                cipherText = enc.Encrypt(plainText, key);
                
                iv = enc.IV;
                key = enc.Key;
                // Look at your cipher text and initialization vector. 
                Console.WriteLine("          Cipher text: " +
                                  Convert.ToBase64String(cipherText));
                Console.WriteLine("Initialization vector: " + Convert.ToBase64String(iv));
                Console.WriteLine("                  Key: " + Convert.ToBase64String(key));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception encrypting. " + ex.Message);
                return;
            }
            try
            { 
                Decryptor dec = new Decryptor(algorithm);
                dec.IV = iv;
              
                byte[] plainText = dec.Decrypt(cipherText, key);
              
                Console.WriteLine("           Plain text: " +
                                  Encoding.ASCII.GetString(plainText));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception decrypting. " + ex.Message);
                return;
            }

        }
    }
}